package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 学生管理-API接口
 * @author liuyp
 * @since 2025/7/25
 */
@RestController
@RequestMapping("/students")
public class StudentController {
    @Resource
    private StudentService studentService;

    /**
     * 分页查询学生列表
     */
    @GetMapping
    public Result page(String name, Integer degree, Integer clazzId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        PageBean<Student> pageBean = studentService.page(name, degree, clazzId, page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 新增学生信息
     */
    @PostMapping
    public Result add(@RequestBody Student student){
        studentService.add(student);
        return Result.success();
    }

    /**
     * 根据id查询学生信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        studentService.updateById(student);
        return Result.success();
    }

    /**
     * 根据id删除学生
     */
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable("ids") Integer[] ids){
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable("id") Integer studentId,
                            @PathVariable("score") Integer score){
        studentService.violation(studentId, score);
        return Result.success();
    }
}
